/*
███████╗██╗   ██╗ ██████╗██╗  ██╗
██╔════╝██║   ██║██╔════╝██║ ██╔╝
█████╗  ██║   ██║██║     █████╔╝ 
██╔══╝  ██║   ██║██║     ██╔═██╗ 
██║     ╚██████╔╝╚██████╗██║  ██╗
╚═╝      ╚═════╝  ╚═════╝╚═╝  ╚═╝
*/
package dao;

import common.utils.DBUtilsV2;
import java.math.BigDecimal;
import java.sql.*;

public class PaymentDao {

    // ✅ 管理员充值（使用 sp_admin_recharge）
    public String adminRecharge(int adminId, int targetUserId, BigDecimal amount) throws SQLException {
        String sql = "{CALL sp_admin_recharge(?, ?, ?)}";
        try (Connection conn = DBUtilsV2.open();
             CallableStatement cstmt = conn.prepareCall(sql)) {
            cstmt.setInt(1, adminId);
            cstmt.setInt(2, targetUserId);
            cstmt.setBigDecimal(3, amount);
            cstmt.execute();
            return "SUCCESS";
        }
    }

    // ✅ 用户设置支付密码（使用 sp_user_set_payment_pin）
    public String setPaymentPin(int userId, String oldPin, String newPin) throws SQLException {
        String sql = "{CALL sp_user_set_payment_pin(?, ?, ?)}";
        try (Connection conn = DBUtilsV2.open();
             CallableStatement cstmt = conn.prepareCall(sql)) {
            cstmt.setInt(1, userId);
            cstmt.setString(2, oldPin);
            cstmt.setString(3, newPin);
            cstmt.execute();
            return "SUCCESS";
        }
    }

    // ✅ 用户支付（使用 sp_process_payment）
    public String processPayment(int userId, int orderId, String paymentPin) throws SQLException {
        String sql = "{CALL sp_process_payment(?, ?, ?)}";
        try (Connection conn = DBUtilsV2.open();
             CallableStatement cstmt = conn.prepareCall(sql)) {
            cstmt.setInt(1, userId);
            cstmt.setInt(2, orderId);
            cstmt.setString(3, paymentPin);
            cstmt.execute();
            return "SUCCESS";
        }
    }
    /**
     * 管理员重置用户支付密码或修改钱包状态
     * 直接调用数据库已存在的 sp_admin_manage_user_wallet
     */
    public String adminManageWallet(int userId,
                                    String newPin,
                                    Integer newStatus) throws SQLException {
        String sql = "{CALL sp_admin_manage_user_wallet(?, ?, ?)}";
        try (Connection conn = DBUtilsV2.open();
             CallableStatement cstmt = conn.prepareCall(sql)) {
            cstmt.setInt(1, userId);
            cstmt.setString(2, newPin);
            if (newStatus == null) {
                cstmt.setInt(3, 1);
            } else {
                cstmt.setInt(3, newStatus);
            }
            cstmt.execute();
            return "SUCCESS";
        }
    }
    public ResultSet findWalletByUserId(Integer userId) throws SQLException {
        String sql = "{CALL sp_wallet_find_by_user_id(?)}";
        Connection conn = DBUtilsV2.open();
        CallableStatement cs = conn.prepareCall(sql);
        if (userId == null) {
            cs.setNull(1, Types.INTEGER);
        } else {
            cs.setInt(1, userId);
        }
        return cs.executeQuery(); // 调用方负责关闭
    }
}